package easier.app.x.starter;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;

/**
 * @author Liujin 2019/5/8:13:07:58
 */
public class ActivityLauncher {

    public static ActivityInfo getActivityInfo( Context context, ComponentName componentName ) {

        try {
            return context.getPackageManager().getActivityInfo(
                componentName,
                PackageManager.GET_META_DATA
            );
        } catch ( NameNotFoundException e ) {
            e.printStackTrace();
        }

        return null;
    }

    public static String getLaunchInfo( Activity activity ) {

        ActivityInfo activityInfo = getActivityInfo( activity, activity.getComponentName() );
        return activityInfo == null ?
            "activity: " + activity + "\n\t" + "taskId: " + activity.getTaskId() :
            "activity: " + activity + "\n\t" + "taskId: " + activity.getTaskId()
                + "\n\t" + "taskAffinity: " + activityInfo.taskAffinity;
    }

    /**
     * 按照manifest中的定义启动
     */
    public static void start( Context context, Class< ? extends Activity > activityClass ) {

        start( context, activityClass, null );
    }

    /**
     * 按照manifest中的定义启动
     */
    public static void start( Context context, Class< ? extends Activity > activityClass, Bundle extra ) {

        Intent intent = new Intent( context, activityClass );
        if ( extra != null && !extra.isEmpty() ) {
            intent.putExtras( extra );
        }
        context.startActivity( intent );
    }

    /**
     * 相当于android:launchMode="standard"
     */
    public static void startStand( Context context, Class< ? extends Activity > activityClass ) {

        startStand( context, activityClass, null );
    }

    /**
     * 相当于android:launchMode="standard"
     */
    public static void startStand( Context context, Class< ? extends Activity > activityClass, Bundle extra ) {

        Intent intent = new Intent( context, activityClass );
        if ( extra != null && !extra.isEmpty() ) {
            intent.putExtras( extra );
        }
        intent.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK );
        context.startActivity( intent );
    }

    /**
     * 相当于android:launchMode="singleTop"
     */
    public static void startSingleTop( Context context, Class< ? extends Activity > activityClass ) {

        startSingleTop( context, activityClass, null );
    }

    /**
     * 相当于android:launchMode="singleTop"
     */
    public static void startSingleTop( Context context, Class< ? extends Activity > activityClass, Bundle extra ) {

        Intent intent = new Intent( context, activityClass );
        if ( extra != null && !extra.isEmpty() ) {
            intent.putExtras( extra );
        }
        intent.setFlags( Intent.FLAG_ACTIVITY_SINGLE_TOP );
        context.startActivity( intent );
    }

    /**
     * android:launchMode="singleTask"
     */
    public static void startSingleTask( Context context, Class< ? extends Activity > activityClass ) {

        startSingleTask( context, activityClass, null );
    }

    /**
     * android:launchMode="singleTask"
     */
    public static void startSingleTask( Context context, Class< ? extends Activity > activityClass, Bundle extra ) {

        Intent intent = new Intent( context, activityClass );
        if ( extra != null && !extra.isEmpty() ) {
            intent.putExtras( extra );
        }
        intent.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );
        context.startActivity( intent );
    }

    /**
     * 新建一个任务栈(肯定与当前任务栈不同),启动该activity,该activity位于新启动的任务栈栈底
     */
    public static void startMultipleTask( Context context, Class< ? extends Activity > activityClass ) {

        startMultipleTask( context, activityClass, null );
    }

    /**
     * 新建一个任务栈(肯定与当前任务栈不同),启动该activity,该activity位于新启动的任务栈栈底
     */
    public static void startMultipleTask( Context context, Class< ? extends Activity > activityClass, Bundle extra ) {

        Intent intent = new Intent( context, activityClass );
        if ( extra != null && !extra.isEmpty() ) {
            intent.putExtras( extra );
        }
        intent.setFlags( Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_TASK );
        context.startActivity( intent );
    }

    /**
     * 如果该activity存在,那么清理该activity之上的所有activity,并且回调onNewIntent(Intent
     * 如果该activity不存在那么创建新的,位于栈顶
     */
    public static void clearTop( Context context, Class< ? extends Activity > activityClass ) {

        clearTop( context, activityClass, null );
    }

    /**
     * 如果该activity存在,那么清理该activity之上的所有activity,并且回调onNewIntent(Intent
     * 如果该activity不存在那么创建新的,位于栈顶
     */
    public static void clearTop( Context context, Class< ? extends Activity > activityClass, Bundle extra ) {

        Intent intent = new Intent( context, activityClass );
        if ( extra != null && !extra.isEmpty() ) {
            intent.putExtras( extra );
        }
        intent.setFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP );
        context.startActivity( intent );
    }

    /**
     * 清空任务栈,创建新的activity
     */
    public static void clearTask( Context context, Class< ? extends Activity > activityClass ) {

        clearTask( context, activityClass, null );
    }

    /**
     * 清空任务栈,创建新的activity
     */
    public static void clearTask( Context context, Class< ? extends Activity > activityClass, Bundle extra ) {

        Intent intent = new Intent( context, activityClass );
        if ( extra != null && !extra.isEmpty() ) {
            intent.putExtras( extra );
        }
        intent.setFlags( Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK );
        context.startActivity( intent );
    }
}
